package controller;

import java.util.ArrayList;
import java.util.Collection;

import model.OfferedCourse;
import model.Registration;
import model.Student;
import model.container.RegistrationContainer;

public class RegistrationController {
	/* Registration controller instance */
	private static RegistrationController instance = new RegistrationController();
	
	/* Constructs a new registration controller */
	private RegistrationController(){
	}
	
	/** Returns an instance of the registration controller */
	public static RegistrationController getInstance(){
		return instance;
	}
	
	/** Adds a new registration */
	public void addRegistration(Registration registration) throws Exception{
		int size = RegistrationContainer.getInstance().getRegistrationByOfferedCourse(registration.getOfferedCourse().getPrimaryKey()).size();
		if(registration.getOfferedCourse().getNumberOfPlaces() > size){
			if(!registration.getOfferedCourse().getRegistrations().add(registration)){
				//TODO create better registration data and replace Hashset with treeset.
				throw new Exception("Already registered for this course");
			}
			registration.getStudent().getRegistrations().add(registration);
			registration.getOfferedCourse().getRegistrations().add(registration);
			RegistrationContainer.getInstance().getRegistrations().add(registration);
		}else{
			throw new Exception("Course is full. Better luck next year!");
		}
	}
}